# $Id :  $



# Copyright 2008-2010, Pascal Fautrero & JCharles
#
# This file is part of PyArchiver.
# 
# PyArchiver is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyArchiver is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PyArchiver.  If not, see <http://www.gnu.org/licenses/>.



# Import packages

import wx                        # This module uses the new wx namespace
import sys
import os
import wx.combo

import information_dialog

import ConfigParser

# import images                  # For PyEmbeddedImage

_ = wx.GetTranslation            # Define _ or add _ to builtins in your app file

appDir = os.path.split(os.path.abspath(sys.argv[0]))[0]

#---------------------------------------------------------------------------
        
class My_TabDrive(wx.Panel):       
    def __init__(self, parent, frame):
        wx.Panel.__init__(self, parent, -1,
                          style=wx.TAB_TRAVERSAL | wx.BORDER_THEME)
         
        #-------------------------------------------------------------------
        
        self.parent = parent
        self.frame = frame
        
        #-------------------------------------------------------------------

        if wx.Platform == "__WXMSW__":
            color = self.frame.panel.GetBackgroundColour()
            self.SetBackgroundColour(color)
        else:
            pass
            
        #-------------------------------------------------------------------

        # Simplified init method
        self.create_widget()
        self.do_layout()

    #----------------------------------------------------------------------- 

    def create_widget(self):
        self.txt = wx.StaticText(self, -1, _("Select the source whose contents must be recorded."))
        
       # Initial construction
        self.dirCtrl = wx.GenericDirCtrl(self, -1, size=(370, 183),
                                         style=wx.DIRCTRL_DIR_ONLY |
                                         wx.DIRCTRL_3D_INTERNAL)
             
        # Create the image list (and defined image size)
        il = wx.ImageList(16, 16)

        # Add images to list. You need to keep the same 
        # order in order for this to work!

        # Images order : Closed folder, Open folder, Root of file system (linux),
        # Drive letter (windows), Cdrom drive, Removable drive on win98
        # Removable drive (floppy, flash, etc)
        bitmapDir = os.path.join(appDir, "bitmaps")
        il.Add(wx.Bitmap(os.path.join(bitmapDir, "dirCtrl_folder.png"), wx.BITMAP_TYPE_PNG))
        il.Add(wx.Bitmap(os.path.join(bitmapDir, "dirCtrl_folder_open.png"), wx.BITMAP_TYPE_PNG))
        il.Add(wx.Bitmap(os.path.join(bitmapDir, "dirCtrl_root.png"), wx.BITMAP_TYPE_PNG))
        il.Add(wx.Bitmap(os.path.join(bitmapDir, "dirCtrl_hdd.png"), wx.BITMAP_TYPE_PNG))
        il.Add(wx.Bitmap(os.path.join(bitmapDir, "dirCtrl_cdrom.png"), wx.BITMAP_TYPE_PNG))
        il.Add(wx.Bitmap(os.path.join(bitmapDir, "dirCtrl_floppy.png"), wx.BITMAP_TYPE_PNG))
        il.Add(wx.Bitmap(os.path.join(bitmapDir, "dirCtrl_floppy.png"), wx.BITMAP_TYPE_PNG))
        # or PyEmbeddedImage
        # il.Add(images.DirCtrl_folder.GetBitmap())
        # il.Add(images.DirCtrl_folder_open.GetBitmap())
        # il.Add(images.DirCtrl_root.GetBitmap())
        # il.Add(images.DirCtrl_hdd.GetBitmap())
        # il.Add(images.DirCtrl_cdrom.GetBitmap())
        # il.Add(images.DirCtrl_floppy.GetBitmap())
        # il.Add(images.DirCtrl_floppy.GetBitmap())
        
        # Assign image list
        self.dirCtrl.GetTreeCtrl().SetImageList(il)
        self.il = il
        
        self.btnSave = wx.Button(self, id=wx.ID_SAVE, label="")
        
        # Bind a button event to an event handler
        self.Bind(wx.EVT_BUTTON, self.on_get_path, self.btnSave)

        self.btnSave.SetFocus()
        
    #-----------------------------------------------------------------------

    def do_layout(self):
        txtSizer = wx.BoxSizer(wx.HORIZONTAL)
        dirCtrlSizer = wx.BoxSizer(wx.HORIZONTAL)
        btnSizer = wx.BoxSizer(wx.HORIZONTAL)
        mainSizer = wx.BoxSizer(wx.VERTICAL)

        #----------

        txtSizer.Add(self.txt, 1, wx.ALL | wx.EXPAND, 10)        
        dirCtrlSizer.Add(self.dirCtrl, 1, wx.EXPAND, 0)
        btnSizer.Add(self.btnSave, 0, wx.TOP | wx.BOTTOM | wx.RIGHT, 10)
        mainSizer.Add(txtSizer, 0, wx.EXPAND, 0)
        mainSizer.Add(dirCtrlSizer, 1, wx.EXPAND, 0)
        mainSizer.Add(btnSizer, 0, wx.ALIGN_RIGHT)

        #----------
        
        self.SetAutoLayout(True)
        self.SetSizer(mainSizer)
        mainSizer.Fit(self)

    #----------------------------------------------------------------------- 
            
    def on_get_path(self, event):
        myPath = self.dirCtrl.GetPath()
        #print ""
        #print "Path source : %s" % (myPath)

        configDir = os.path.join(appDir, "config")
        self.file_cfg = os.path.join(configDir, "Source.cfg")
        # or 
        # path = os.path.abspath(sys.argv[0])
        # configDir = os.path.join(os.path.split(path)[0], "config")
        # self.file_cfg = os.path.join(configDir, "Source.cfg")
        
        self.config = ConfigParser.ConfigParser()
        if len(self.config.read([self.file_cfg])) == 0:
            self.config.add_section("SOURCE")             # [SOURCE]
            self.config.set("SOURCE", "drive1", myPath)   # drive1 = C:\

            
            file_cfg = open(self.file_cfg, "wb")          # for python 2.5 /2.6
            self.config.write(file_cfg)
            file_cfg.close()
        print ""
        print "Set path source on : %s" % (myPath)
        
        dlg = information_dialog.My_InformationDialog(self,
                                                      message=_("\n Preferences Saved !"),
                                                      title=_("Information"),
                                                      pos=(-1, -1))
        dlg.ShowModal()
        
#---------------------------------------------------------------------------

##class My_TabAppearance(wx.Panel):
##    def __init__(self, parent, frame):
##        wx.Panel.__init__(self, parent, -1,
##                          style=wx.TAB_TRAVERSAL | wx.BORDER_THEME)

        #-------------------------------------------------------------------
        
##        self.parent = parent
##        self.frame = frame
        
        #-------------------------------------------------------------------

##        if wx.Platform == "__WXMSW__":
##            color = self.frame.panel.GetBackgroundColour()
##            self.SetBackgroundColour(color)
##        else:
##            pass

        #-------------------------------------------------------------------

        # Simplified init method
#        self.create_widget()
##        self.do_layout()

    #----------------------------------------------------------------------- 

##    def create_widget(self):
##        self.txt1 = wx.StaticText(self, -1, _("Determine the size of the window."))
        
##        self.txtWidth = wx.StaticText(self, -1, _("Width :"))
##        font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
##        font.SetWeight(wx.BOLD)
##        self.txtWidth.SetFont(font)
        
##        self.txtHeight = wx.StaticText(self, -1, _("Height :"))
##        font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
##        font.SetWeight(wx.BOLD)
##        self.txtHeight.SetFont(font)

##        w, h = self.frame.GetSizeTuple()
##        self.spin1 = wx.SpinCtrl(self, -1, str(w), (-1, -1), (120, -1), min=600, max=1024)
##        self.spin2 = wx.SpinCtrl(self, -1, str(h), (-1, -1), (120, -1), min=500, max=768)
        
##        self.txt2 = wx.StaticText(self, -1, _("The settings take effect after you restart the program."))
        
##        self.line = wx.StaticLine(self, -1, size = (400, -1), style=wx.LI_HORIZONTAL)

##        self.btnSave = wx.Button(self, id=wx.ID_SAVE, label="")
        
        # Bind a button event to an event handler
##        self.Bind(wx.EVT_BUTTON, self.OnSave, self.btnSave)

    #-----------------------------------------------------------------------

##    def do_layout(self): 
##        txtSizer1 = wx.BoxSizer(wx.HORIZONTAL)
##        txtSizer2 = wx.BoxSizer(wx.HORIZONTAL)
##        txtSizer3 = wx.BoxSizer(wx.HORIZONTAL)
##        spinSizer1 = wx.BoxSizer(wx.HORIZONTAL)
##        spinSizer2 = wx.BoxSizer(wx.HORIZONTAL)

##        txtSizer4 = wx.BoxSizer(wx.HORIZONTAL)
##        lineSizer = wx.BoxSizer(wx.HORIZONTAL)
##        btnSizer = wx.BoxSizer(wx.HORIZONTAL)
##        mainSizer = wx.BoxSizer(wx.VERTICAL)

        #----------
    
##        txtSizer1.Add(self.txt1, 1, wx.TOP | wx.LEFT | wx.EXPAND, 10)
##        txtSizer2.Add(self.txtWidth, 0, wx.LEFT | wx.TOP | wx.EXPAND, 25)
##        spinSizer1.Add(self.spin1, 0, wx.LEFT, 25)
##        txtSizer3.Add(self.txtHeight, 0, wx.LEFT | wx.TOP | wx.EXPAND, 25)
##        spinSizer2.Add(self.spin2, 0, wx.LEFT, 25)
##        txtSizer4.Add(self.txt2, 0, wx.ALL | wx.EXPAND, 25)        
##        lineSizer.Add(self.line, 1, wx.ALL, 0)
##        btnSizer.Add(self.btnSave, 0, wx.ALL | wx.RIGHT, 10)
##        mainSizer.Add(txtSizer1, 0, wx.EXPAND, 5)
##        mainSizer.Add(txtSizer2, 0, wx.BOTTOM | wx.EXPAND, 5)
##        mainSizer.Add(spinSizer1, 0, wx.EXPAND, 5)
##        mainSizer.Add(txtSizer3, 0, wx.BOTTOM | wx.EXPAND, 5)
##        mainSizer.Add(spinSizer2, 0, wx.EXPAND, 5)
##        mainSizer.Add(txtSizer4, 0, wx.EXPAND, 5)
##        mainSizer.Add((10, 0))
##        mainSizer.Add(lineSizer, 1, wx.ALIGN_CENTRE | wx.EXPAND, 0)
##        mainSizer.Add(btnSizer, 0, wx.ALIGN_RIGHT)
        
        #----------
        
##        self.SetAutoLayout(True)
##        self.SetSizer(mainSizer)
##        mainSizer.Fit(self)
        
    #-----------------------------------------------------------------------
                
##    def OnSave(self, event):
##        self.cfg = wx.Config("size")
##        self.cfg.WriteInt("Width", self.spin1.GetValue())
##        self.cfg.WriteInt("Height", self.spin2.GetValue())

##        print "Set frame width on : %d point(s)" % (self.spin1.GetValue())
##        print "Set frame height on : %d point(s)" % (self.spin2.GetValue())
        # print("Frame width :", self.spin1.GetValue(), "point")
        # print("Frame height :", self.spin2.GetValue(), "point")


#        self.file_cfg = os.path.join(appdir, "Config\ConfigTesft.cfg")

#        self.config = ConfigParser.ConfigParser()

#        if len(self.config.read([self.file_cfg])) == 0:
#            self.config.add_section("SIZE")  # [SIZE]
#            self.config.set("SIZE", "width", self.spin1.GetValue())  # width = 900
#            self.config.set("SIZE", "height", self.spin2.GetValue())  # height = 600
            
#            print("Frame width :", self.spin1.GetValue(), "point")
#            print("Frame height :", self.spin2.GetValue(), "point")          
#            file_cfg = open(self.file_cfg, 'wb')
#            self.config.write(file_cfg)
#            file_cfg.close()
            
#---------------------------------------------------------------------------

class My_TabLanguage(wx.Panel):
    def __init__(self, parent, frame):
        wx.Panel.__init__(self, parent, -1,
                          style=wx.TAB_TRAVERSAL | wx.BORDER_THEME)

        #-------------------------------------------------------------------
        
        self.parent = parent
        self.frame = frame
        
        #-------------------------------------------------------------------

        if wx.Platform == "__WXMSW__":
            color = self.frame.panel.GetBackgroundColour()
            self.SetBackgroundColour(color)
        else:
            pass

        #-------------------------------------------------------------------

        # Simplified init method
        self.create_widget()
        self.do_layout()

    #----------------------------------------------------------------------- 

    def create_widget(self):
        langList = ["French", "English"]

        bitmapDir = os.path.join(appDir, "bitmaps")
        bmp = wx.Bitmap(os.path.join(bitmapDir, "flag_English.png"))
        bmp1 = wx.Bitmap(os.path.join(bitmapDir, "flag_French.png"))
        # or PyEmbeddedImage        
        # bmp = images.Flag_English.GetBitmap()
        # bmp1 = images.Flag_French.GetBitmap()
        
        self.txt1 = wx.StaticText(self, -1, _("Translate the user interface."))
        self.txt2 = wx.StaticText(self, -1, _("Language : "))
        font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
        font.SetWeight(wx.BOLD)
        self.txt2.SetFont(font)
        
        # This combobox is created with a preset list of values.
        self.cb = wx.combo.BitmapComboBox(self, -1, value="", size=(200, -1),
                                          choices=langList, style=wx.CB_READONLY|
                                          wx.CB_SORT|wx.TE_PROCESS_ENTER)

        self.cb.SetItemBitmap(0, bmp)
        self.cb.SetItemBitmap(1, bmp1)
        
        # Bind an ComboBox event to an event handler
        self.Bind(wx.EVT_COMBOBOX, self.evt_combo_box, self.cb)

        configDir = os.path.join(appDir, "config")
        self.file_cfg = os.path.join(configDir, "Config.cfg")
        config = wx.Config("self.file_cfg")
        language = config.Read("Language", "French")
        
#        self.config = ConfigParser.ConfigParser()
#        self.config.read("Config\Config.cfg")	
#        lang_name = config.get("Language")            
        self.cb.SetValue(language)
        
#        self.line = wx.StaticLine(self, -1, size = (400, -1), style=wx.LI_HORIZONTAL)

        self.txt3 = wx.StaticText(self, -1, _("The settings take effect after you restart the application."))
        
#        self.btnSave1 = wx.Button(self, id=wx.ID_OK, label="")
#        self.btnSave1.SetFocus()
        
    #-----------------------------------------------------------------------
        
    def do_layout(self): 
        txt1Sizer = wx.BoxSizer(wx.HORIZONTAL)
        comboTxtSizer = wx.BoxSizer(wx.HORIZONTAL)
#        lineSizer = wx.BoxSizer(wx.HORIZONTAL)
#        btnSizer = wx.BoxSizer(wx.HORIZONTAL)
        mainSizer = wx.BoxSizer(wx.VERTICAL)

        #----------
      
        txt1Sizer.Add(self.txt1, 1, wx.TOP | wx.LEFT | wx.EXPAND, 10)
        comboTxtSizer.Add(self.txt2, 0, wx.ALL | wx.EXPAND, 5)          
        comboTxtSizer.Add(self.cb, 1, wx.EXPAND, 0)
#        lineSizer.Add(self.line, 1, wx.ALL, 0)
#        btnSizer.Add(self.btnSave1, 0, wx.ALL | wx.RIGHT, 10)
        mainSizer.Add(txt1Sizer, 0, wx.EXPAND, 5)
        mainSizer.Add(comboTxtSizer, 0, wx.ALL, 20)
        mainSizer.Add(self.txt3, 0, wx.LEFT, 25)
#        mainSizer.Add((10, 10))
#        mainSizer.Add(lineSizer, 1, wx.ALIGN_CENTRE | wx.EXPAND, 0)
#        mainSizer.Add(btnSizer, 0, wx.ALIGN_RIGHT)
        
        #----------
        
        self.SetAutoLayout(True)
        self.SetSizer(mainSizer)
        mainSizer.Fit(self)
        
    #-----------------------------------------------------------------------
        
    def evt_combo_box(self, event):
        progLoc = os.getcwd()
        configLoc = os.path.join(progLoc, "Config\Config.cfg")
        
        self.config = wx.FileConfig(appName="",
                                    vendorName="",
                                    localFilename=configLoc,
                                    style=wx.CONFIG_USE_LOCAL_FILE)
        langApp = self.config.Read("Language")

        lang = self.cb.GetValue()
        if lang == "French":
            print ""
            print "Program language : %s --> %s" % (langApp, "French")
            print "Set language on : %s --> %s" % (lang, "fr")
            self.frame.on_switch_lang_fr()
        
        elif lang == "English":
            print ""
            print "Program language : %s --> %s" % (langApp, "English")
            print "Set language on : %s --> %s" % (lang, "en")
            self.frame.on_switch_lang_en()

        dlg = information_dialog.My_InformationDialog(self,
                                                      message=_("\n Preferences Saved !"),
                                                      title=_("Information"),
                                                      pos=(-1, -1))
        dlg.ShowModal()

#---------------------------------------------------------------------------

class My_SettingDialog(wx.Dialog):
    def __init__(self, parent):
        wx.Dialog.__init__(self, parent, -1,
                           title=_("Setting"), size=(400, 300))
        
        # Bind the close event to an events handler
        self.Bind(wx.EVT_CLOSE, self.on_close_window)

        #-------------------------------------------------------------------
        
        self.parent = parent

        #-------------------------------------------------------------------
        
        self.panel = wx.Panel(self, -1, style=wx.TAB_TRAVERSAL)
        
        #-------------------------------------------------------------------

        # Simplified init method
        self.create_widget()
        self.do_layout()

        #-------------------------------------------------------------------
        
        self.CenterOnParent(wx.BOTH)
        print ""
        print "Display the setting dialog"
         
        #-------------------------------------------------------------------
        
        self.ShowModal()
        
    #-----------------------------------------------------------------------

    def create_widget(self):         
        self.nb = wx.Notebook(self.panel, style=wx.BK_DEFAULT |wx.TAB_TRAVERSAL)

        drive = My_TabDrive(self.nb, self.parent)
        # size = My_TabAppearance(self.nb, self.parent)
        other = My_TabLanguage(self.nb, self.parent)
        
        # Create the page windows as children of the notebook
        self.nb.AddPage(drive, _("Source"))
        ##  self.nb.AddPage(size, _("Appearance"))
        self.nb.AddPage(other, _("Language"))
        
        # Create the image list (and defined image size)
        il = wx.ImageList(16, 16)

        # Add images to list
        bitmapDir = os.path.join(appDir, "bitmaps")
        idx1 = il.Add(wx.Bitmap(os.path.join(bitmapDir, "tab_Drive.png"), wx.BITMAP_TYPE_PNG))
        # idx2 = il.Add(wx.Bitmap(os.path.join(bitmapDir, "item_Empty.png"), wx.BITMAP_TYPE_PNG))
        idx2 = il.Add(wx.Bitmap(os.path.join(bitmapDir, "tab_Language.png"), wx.BITMAP_TYPE_PNG))
        # or PyEmbeddedImage
        # idx1 = il.Add(images.Drive.GetBitmap())
        # idx2 = il.Add(images.Item_Empty.GetBitmap())
        # idx2 = il.Add(images.Language.GetBitmap())

        # Assign image list
        self.nb.AssignImageList(il)
        
        self.nb.SetPageImage(0, idx1)
        # self.nb.SetPageImage(1, idx2)
        self.nb.SetPageImage(1, idx2)
        
        # Bind some events to an events handler
        self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.on_page_changed)
        self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGING, self.on_page_changing)
        
    #-----------------------------------------------------------------------

    def do_layout(self): 
        notebookSizer = wx.BoxSizer(wx.VERTICAL)
        mainSizer = wx.BoxSizer(wx.VERTICAL)

        #----------
        
        notebookSizer.Add(self.nb, 1, wx.EXPAND, 0)
        mainSizer.Add(notebookSizer, 1, wx.TOP | wx.EXPAND, 5)        

        #----------
        
        self.panel.SetAutoLayout(True)
        self.panel.SetSizer(mainSizer)
        mainSizer.Fit(self)

    #-----------------------------------------------------------------------

    def on_page_changed(self, event):
        old = event.GetOldSelection()
        new = event.GetSelection()
        sel = self.nb.GetSelection()
        event.Skip()


    def on_page_changing(self, event):
        old = event.GetOldSelection()
        new = event.GetSelection()
        sel = self.nb.GetSelection()
        event.Skip()

         
    def on_close_window(self, event):
        self.Destroy()

